package mvc;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Internal class which holds object instance, method and arguments' types.
 * 
 * @author Michael Liao (askxuefeng@gmail.com)
 */
class Action {

    /**
     * Object instance.
     */
    public final Object instance;

    /**
     * Method instance.
     */
    public final Method method;

    /**
     * Method's arguments' types.
     */
    public final Class<?>[] arguments;

    public final Map<String ,ParameterInjector> injectors = new HashMap<String ,ParameterInjector>();
    
    public Action(Object instance, Method method) {
        this.instance = instance;
        this.method = method;
        this.arguments = method.getParameterTypes();
        initInjector();
    }

    /**
     * 初始化参数注射器
     */
    void initInjector(){
    	Annotation[][] ans = method.getParameterAnnotations();
    	Class<?>[] types = method.getParameterTypes();
    	ParameterInjector injector;
    	String argName;
    	Set<String>	keys;
    	
    	for(int i=0 ;i<types.length ;i++){
    		for(Annotation a : ans[i]){
    			if(a.annotationType() == Param.class){
    				argName = ((Param)a).value();
    				if(argName != ""){
    					injector = new ParameterInjector(i,types[i],argName);
    					keys = injector.getKeys();
    					for(String key : keys){
    						injectors.put(key, injector);
    					}
    				}
    			}
    		}
    	}
    }
}
